package com.stox.client.workbench.ui.javafx;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.stox.client.ui.javafx.core.StoxPlatform;
import com.stox.client.workbench.lifecycle.DefaultUncaughtExceptionHandler;
import com.stox.client.workbench.lifecycle.StoxJavaFxClassLoader;
import com.stox.client.workbench.ui.javafx.stage.StoxStage;

public class StoxWorkbench extends Application {

	private static Stage splashStage;
	public static void closeSplashStage(){
		splashStage.close();
	}
	
	private void splash(Stage stage){
		stage.setTitle("STOX");
		stage.setMaximized(true);
		stage.initStyle(StageStyle.TRANSPARENT);
		StackPane pane = new StackPane();
		pane.getChildren().add(createSplashNode());
		stage.setScene(new Scene(pane, Color.TRANSPARENT));
		stage.show();
	}
	
	private Node createSplashNode(){
		Text text = new Text("STOX");
		text.setFont(Font.font("Old English Text MT",FontWeight.EXTRA_BOLD, Screen.getPrimary().getVisualBounds().getMaxY()/4));
		text.setFill(Color.rgb(25, 25, 25, 1));
		HBox hbox = new HBox(text);
		hbox.setAlignment(Pos.CENTER);
		return hbox;
	}
	
	@Override
	public void start(Stage splashStage) throws Exception {
		StoxWorkbench.splashStage = splashStage;
		splash(splashStage);
		StoxPlatform.runLater(new Runnable(){
			@Override
			public void run() {
				Thread.setDefaultUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler());
				Thread.currentThread().setContextClassLoader(new StoxJavaFxClassLoader(Thread.currentThread().getContextClassLoader()));
				
				ApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.stox");
				StoxStage stoxStage = applicationContext.getBean(StoxStage.class);
				stoxStage.show();
			}
		});
	}
	
}
